package com.djq.hhc.exception;

import com.djq.hhc.result.Result;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: dadd7
 * @Date: 2023/02/25/下午 11:21
 * @Description:
 */

@ControllerAdvice
public class GlobalException {

    //统一异常处理
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public Result error(Exception e) {
        e.printStackTrace();
        return Result.fail().message("触发了统一异常处理");
    }

    @ExceptionHandler(ArithmeticException.class)
    @ResponseBody
    public Result error(ArithmeticException e){
        e.printStackTrace();
        return Result.fail().message("触发了特定异常处理");
    }

    @ExceptionHandler(SelfDefinedException.class)
    @ResponseBody
    public Result error(SelfDefinedException e){
        e.printStackTrace();
        return Result.fail().message(e.getMsg()).code(e.getCode());
    }
}

/**
 * //自定义异常类的使用
 * try {
 *     int a = 10/0;
 * }catch(Exception e) {
 *     throw new GgktException(20001,"出现自定义异常");
 * }
 */
